package com.xhx.db.service.impl;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xhx.db.entity.AppVersion;
import com.xhx.db.mapper.AppVersionMapper;
import com.xhx.db.service.AppVersionService;
import com.xhx.util.PageConvertUtil;
import com.xhx.vo.PageInfoParam;
import com.xhx.vo.PageInfoResult;

import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * APP版本Service
 * 
 * @author xhx
 */
@Slf4j
@Service
public class AppVersionServiceImpl implements AppVersionService {

	@Autowired
	private AppVersionMapper appVersionMapper;

	@Override
	public AppVersion getById(long id) {
		return appVersionMapper.getById(id);
	}

	@Override
	public int add(AppVersion entity) {
		entity.setAppVersionId(IdUtil.getSnowflakeNextId());
		return appVersionMapper.insert(entity);
	}

	@Override
	public int deleteByIdFalse(long id) {
		return appVersionMapper.deleteByIdFalse(id);
	}

	@Override
	public int updateById(AppVersion appVersion) {
		return appVersionMapper.updateById(appVersion);
	}

	@Override
	public AppVersion getNewVersion(byte systemType) {
		Map<String, Object> params = new HashMap<>();
		params.put("systemType", systemType);
		params.put("onlineTime", LocalDateTime.now());
		return appVersionMapper.getNewVersion(params);
	}

	@Override
	public AppVersion getByNo(Integer versionNo) {
		if (versionNo == null || versionNo == 0) {
			return null;
		}
		return appVersionMapper.getByNo(versionNo);
	}

	@Override
	public AppVersion getByName(String versionName) {
		if (ObjectUtils.isEmpty(versionName)) {
			return null;
		}
		return appVersionMapper.getByName(versionName);
	}

	@Override
	public PageInfoResult<AppVersion> getPage(PageInfoParam<Map<String, Object>> param) {
		PageHelper.startPage(param.getPageNo(), param.getPageSize());
		List<AppVersion> list = appVersionMapper.getList(param.getParam());
		return PageConvertUtil.build((Page<AppVersion>) list);
	}

}
